<%@page contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@page import="com.cndatacom.teleweb.util.*" %>
<%@page import="java.io.*"%>
<%@page import="java.awt.*" %>
<%@page import="java.util.*" %>
<%@page import="java.sql.*" %>
<%@page import="java.util.regex.*" %>
<%@page import="javax.imageio.ImageIO" %>

<% 
	String targetMatchNo = "20110426";//比赛编号
	
	//获取数据库连接---
	Connection conn = null;
	Statement stmt = null;
	
	String driverClassName = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	String userName = "ccseweb";
	String password = "password";
	try {
		Class.forName(driverClassName);
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
	
	java.util.List list = new ArrayList();
	
	try {
		conn = DriverManager.getConnection(url, userName, password);
		stmt = conn.createStatement();
		
		String webPath = request.getRealPath("/");
		String folderPath = webPath + "/minipics";//缩略图所在的文件夹路劲
		File f = new File(folderPath);
		boolean isDir = f.isDirectory();
		if(!isDir){//目录不存在则先建目录
			try{
				f.mkdirs();
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		File[] files = FileUtil.getFoldFileList(webPath + "/resizeimages");
		
		//更新VOTE_MATCH表中的参赛作品的数量
		int nPicSum = files.length;
		String sumSQL = "update vote_match set pic_sum="+nPicSum+" where match_no='"+targetMatchNo+"'";
		stmt.executeUpdate(sumSQL);
		stmt.close();
		
		String pic_path = "";
		String minipic_path = "";
		
		for(int i=0; i<files.length; i++) {
			Boolean b = true;
			File file = files[i];
			String fileName = file.getName();//指定文件夹下的图片名字
			
			File[] filesTemps = FileUtil.getFoldFileList(folderPath);
			for(int j=0;j<filesTemps.length;j++) {
				File filesTemp = filesTemps[j];
				String fileTempName = filesTemp.getName();
				if(fileTempName.equals(fileName)) {
					b = false;
					break;
				}
			}
			
			if(b) {
				//判断图片名称有没中文，如果有中文则系统自动更换图片的名字
				if(regexFileName(fileName)) {
					String randomFileName = randomFileName(fileName);
					renameFile(webPath+"/resizeimages/"+fileName, webPath+"/resizeimages/"+randomFileName);
					fileName = randomFileName;
				}
				
				String fromPath = webPath + "/resizeimages/" + fileName;
				String toPath = folderPath + "/" + fileName;
				pic_path = "resizeimages/" + fileName;
				minipic_path = "minipics/" + fileName;
				
				new CreatePreviewImageUtil().saveImageAsJpg(fromPath,toPath,140,140);
				String picSQL = "insert into vote_picinfo (pic_path,minipic_path) values ('"+pic_path+"','"+minipic_path+"')";//插入图片信息的SQL语句，插入原图相对路劲和缩略图的相对路劲
				stmt = conn.createStatement();
				stmt.executeUpdate(picSQL);
			}
			
		}
		
	}catch (SQLException e) {
		e.printStackTrace();
	}finally {
		stmt.close();
		conn.close();
	}

%>

<html>
	<head>
		<title>摄影大赛投票参赛图片数据初始化</title>
	</head>
	
	<body>
	<br/><br/>
		<center>
			<h3>数据初始化成功……</h3>
		</center>
	</body>
</html>
<%!
	public static Boolean regexFileName(String fileName) {
		String pattern = "[\\u4e00-\\u9fa5]";   
		Pattern p = Pattern.compile(pattern);   
		Matcher result = p.matcher(fileName);  
		return result.find();
	}
	
	public static String randomFileName(String fileName) {
		Long randomName = new java.util.Date().getTime();
		String fileType = fileName.substring(fileName.indexOf("."));
		return randomName + fileType;
	}
	
	public static Boolean renameFile(String oldName, String newName) {
		File newFile = new File(newName);
		File oldFile = new File(oldName);
		oldFile.renameTo(newFile);
		return true;
	}
%>